package com.wy.service.impl.wy;

import com.alibaba.fastjson.JSONArray;
import com.wy.bean.HouseholdBean;
import com.wy.constants.CommonConstants;
import com.wy.constants.CommonValues;
import com.wy.controller.wy.HouseholdController;
import com.wy.dao.CarsMapper;
import com.wy.dao.HouseholdsMapper;
import com.wy.model.sys.Cars;
import com.wy.model.sys.Households;
import com.wy.model.sys.Residence;
import com.wy.model.sys.UserEntity;
import com.wy.service.sys.UserService;
import com.wy.service.wy.HouseholdService;
import com.wy.service.wy.ResidenceService;
import com.wy.utils.Base64Util;
import com.wy.utils.FileUtil;
import com.wy.utils.ImageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service("householdServiceImpl")
public class HouseholdServiceImpl implements HouseholdService {

	private Logger log = LoggerFactory.getLogger(HouseholdServiceImpl.class);
	@Autowired
	private HouseholdsMapper householdDao;

	@Autowired
	private CarsMapper carsMapper;

	@Autowired
	private CommonValues commonValues;

	@Resource(name = "userServiceImpl")
	private UserService userService;

	@Resource(name = "residenceServiceImpl")
	private ResidenceService residenceService;

	@Override
	public List<HouseholdBean> householdList(Integer pageSize, Integer start, Integer residenceId, Integer buildingId, Integer houseId, String householdName,String authStatus,String mobile,String plateNum,List<String> rids) {
		List<HouseholdBean> list = householdDao.householdList(pageSize,start,residenceId,buildingId,houseId,householdName,authStatus,mobile,plateNum,rids);

		List<Cars> carsList = null;
		List<String> cars = null;
		List<HouseholdBean> familyMembers = null;
		String tempPath = "";
		for(HouseholdBean householdBean : list) {
			//注意#号在url有特殊含义，需要进行转义
			//不同类型的图片从不同目录下读取
//			if(!StringUtils.isEmpty(householdBean.getIdCardImage1())) {
//				tempPath = String.format(commonValues.getPicPath(),path,householdBean.getProjectName(), CommonConstants.CARD);
//				householdBean.setIdCardImage1((tempPath + householdBean.getIdCardImage1()).replace("#","%23"));
//			}
//			if(!StringUtils.isEmpty(householdBean.getIdCardImage2())) {
//				tempPath = String.format(commonValues.getPicPath(),path,householdBean.getProjectName(), CommonConstants.CARD);
//				householdBean.setIdCardImage2((tempPath + householdBean.getIdCardImage2()).replace("#","%23"));
//			}
//			if(!StringUtils.isEmpty(householdBean.getFacePic())) {
//				tempPath = String.format(commonValues.getPicPath(),path,householdBean.getProjectName(), CommonConstants.FACE);
//				householdBean.setFacePic((tempPath + householdBean.getFacePic()).replace("#","%23"));
//			}
//			if(!StringUtils.isEmpty(householdBean.getExt6())) {
//				tempPath = String.format(commonValues.getPicPath(),path,householdBean.getProjectName(), CommonConstants.CARD_FACE);
//				householdBean.setExt6((tempPath + householdBean.getExt6()).replace("#","%23"));
//			}

			//显示标签
			if(!StringUtils.isEmpty(householdBean.getExt4())){
				householdBean.setTags(Arrays.asList(householdBean.getExt4().split(",")));
			}

			//显示车辆
			carsList = carsMapper.getCarsByHid(householdBean.getId());
			cars = new ArrayList<String>();
			for(Cars car : carsList) {
				cars.add(car.getPlateNum());
			}
			householdBean.setCarInfos(cars);
			if(cars.size() >0 ) {
				householdBean.setCarInfos_str(String.join(",", cars));
			}
			//显示家属
			familyMembers = householdDao.getHouseholdByHidNotCurrentId(householdBean.getHouseId(),householdBean.getId());
			householdBean.setFamilyMembers(JSONArray.toJSONString(familyMembers));
		}
		return list;
	}

	@Override
	public Integer householdSize(Integer pageSize, Integer start, Integer residenceId,Integer buildingId, Integer houseId,String householdName,String authStatus,String mobile,String plateNum,List<String> rids) {
		return householdDao.householdSize(pageSize,start,residenceId,buildingId,houseId,householdName,authStatus,mobile,plateNum,rids);
	}

	@Override
	public List<HouseholdBean> getHouseholdByHidCreNo(Integer houseId, String credentialNo) {
		return householdDao.getHouseholdByHidCreNo(houseId,credentialNo);
	}

	@Override
	public String insertHousehold(HouseholdBean householdEntity) {
		String messgae = "";
		String tags = "";
		//处理人员标签
		if(!StringUtils.isEmpty(householdEntity.getTags())){
			tags = String.join(",", householdEntity.getTags());
		}
		//处理图片的路径
		householdEntity = dealHouseholdBeanImg(householdEntity);
		householdEntity.setExt4(tags);
		householdEntity.setCreateTime(new Date());
		householdEntity.setModifyTime(new Date());
		householdDao.insert(householdEntity);

//		userService.insertUser(generateUser(householdEntity));

		//判断车辆是否重复车牌号集合
		List<String> existsCars = new ArrayList<String>();

		//解析家庭成员
		if(!StringUtils.isEmpty(householdEntity.getFamilyMembers())) {
			JSONArray familyMemberArray = JSONArray.parseArray(householdEntity.getFamilyMembers());
			HouseholdBean familyMember = null;
			for(int i = 0 ;i<familyMemberArray.size() ;i++ ) {
				try {
					familyMember = new HouseholdBean();
					familyMember.setHouseholdName(familyMemberArray.getJSONObject(i).getString("householdName"));
					familyMember.setGender(familyMemberArray.getJSONObject(i).getString("gender"));
					familyMember.setBirthDate(familyMemberArray.getJSONObject(i).getString("birthDate"));
					familyMember.setCredentialNo(familyMemberArray.getJSONObject(i).getString("credentialNo"));
					familyMember.setMobile(familyMemberArray.getJSONObject(i).getString("mobile"));
					familyMember.setMemberRelation(familyMemberArray.getJSONObject(i).getString("memberRelation"));
					familyMember.setIdCardImage1(familyMemberArray.getJSONObject(i).getString("idCardImage1"));
					familyMember.setIdCardImage2(familyMemberArray.getJSONObject(i).getString("idCardImage2"));
					familyMember.setFacePic(familyMemberArray.getJSONObject(i).getString("facePic"));
					familyMember.setCompany(familyMemberArray.getJSONObject(i).getString("company"));
					familyMember.setAddress(familyMemberArray.getJSONObject(i).getString("address"));
					familyMember.setPeopleType(familyMemberArray.getJSONObject(i).getString("peopleType"));
					familyMember.setExt3(familyMemberArray.getJSONObject(i).getString("ext3"));
					familyMember.setExt4(getExt4Str(familyMemberArray.getJSONObject(i).getJSONArray("ext4")));
					familyMember.setExt5(familyMemberArray.getJSONObject(i).getString("ext5"));
					//处理图片的路径
					familyMember = dealHouseholdBeanImg(familyMember);
					familyMember.setNation(familyMemberArray.getJSONObject(i).getString("nation"));
					familyMember.setHouseId(householdEntity.getHouseId());
					householdDao.insert(familyMember);
					//添加相关登录用户记录
//					userService.insertUser(generateUser(familyMember));

					//解析车辆信息
					String plateNum = familyMemberArray.getJSONObject(i).getString("carInfos");
					Cars car = new Cars();
					car.setHouseholdId(familyMember.getId());
					car.setPlateNum(plateNum);
					carsMapper.insert(car);

					//如果已经包含此条记录
					if (existsCars.contains(plateNum)) {
						TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
						messgae = "车牌号：" + plateNum + "已存在，请重新输入";
						break;
					} else {
						existsCars.add(plateNum);
					}
				}catch(Exception ex) {
					ex.printStackTrace();
				}
			}
		}

		//如果message不为空，则直接返回不需要再执行下面操作
		if(!StringUtils.isEmpty(messgae)){
			return messgae;
		}

		//解析车辆信息
		if(null != householdEntity.getCarInfos() && householdEntity.getCarInfos().size() > 0) {
			List<Cars> carsList = new ArrayList<>();
			Cars car = null;
			for(String plateNum : householdEntity.getCarInfos()) {
				car = new Cars();
				car.setHouseholdId(householdEntity.getId());
				if(StringUtils.isEmpty(plateNum) || StringUtils.isEmpty(plateNum.trim())){
					continue;
				}
				car.setPlateNum(plateNum);
				carsList.add(car);
				//如果已经包含此条记录，则事务回滚，返回结果
				if(existsCars.contains(plateNum)){
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					messgae = "车牌号："+ plateNum+"已存在，请重新输入";
					break;
				}else {
					existsCars.add(plateNum);
				}
			}
			if(null != carsList && carsList.size() > 0) {
				carsMapper.insertBatch(carsList);
			}
		}
		return messgae;
	}


//	private UserEntity generateUserEntity(HouseholdBean householdBean){
//		UserEntity userEntity = new UserEntity();
//		userEntity.setLoginName(householdBean.getMobile());
//		userEntity.setName(householdBean.getHouseholdName());
//		userEntity.setRid();
//		userEntity.setAuthTime();
//		userEntity.setCreateTime(new Date());
//		userEntity.setStatus();
//		userEntity.setPassword();
//
//		return userEntity;
//
//
//
//	}
//	@Override
//	public void insertHousehold(HouseholdBean householdEntity) {
//		householdDao.insert(householdEntity);
//	}

	/**
	 * 手机端补全用户数据
	 * @param userId
	 * @param houseId
	 * @param householdName
	 * @param gender
	 * @param birthDate
	 * @param address
	 * @param peopleType
	 * @param company
	 * @param department
	 * @param identityType
	 * @param credentialType
	 * @param credentialNo
	 * @param idCardImage1
	 * @param idCardImage2
	 * @param facePic
	 * @param ext3
	 * @param ext4
	 * @param nation
	 * @param familyMembers
	 * @param carInfos
	 * @param ext5
	 * @param houseOwnerName
	 * @param houseOwnerCredentialNo
	 * @param houseOwnerMobile
	 * @param currentUserId
	 * @return
	 */
	@Override
	public HouseholdBean registerHousehold(Integer userId, Integer houseId,String householdName, String gender, String birthDate, String address, String peopleType, String company,
								  String department, String identityType, String credentialType, String credentialNo, String idCardImage1,
								  String idCardImage2, String facePic, String ext3, String ext4, String nation, String familyMembers,
										String carInfos,String ext5,
										String houseOwnerName,String houseOwnerCredentialNo,String houseOwnerMobile,Integer currentUserId,String loginMobile,String memberRelation) {
		boolean isNew = true;
		HouseholdBean household = null;
		if(null == userId){
			log.error("user id is empty,create a new household");
			household = new HouseholdBean();
			household.setCreateTime(new Date());
			household.setModifyTime(new Date());
		} else {
			//根据手机号查询是否已经有这个住户信息，如果有，则修改数据，如果没有则新增
			List<HouseholdBean> households = householdDao.getHouseHoldByUserId(userId);
			if (null == households || households.isEmpty()) {
				log.error("user id is not empty "+userId+" but canot find households ,create a new household");
				household = new HouseholdBean();
				household.setCreateTime(new Date());
				household.setModifyTime(new Date());
			} else {
				log.error("user id is not empty "+userId+" update household");
				household = households.get(0);
				household.setModifyTime(new Date());
				isNew = false;
			}
		}
		household.setHouseId(houseId);
		household.setFacePic(FileUtil.getPicName(facePic));
		household.setIdCardImage1(FileUtil.getPicName(idCardImage1));
		household.setIdCardImage2(FileUtil.getPicName(idCardImage2));
		household.setHouseholdName(householdName); //业主姓名
		household.setMobile(loginMobile);//手机号码
		household.setHouseholdName(householdName);
		household.setGender(gender);
		household.setBirthDate(birthDate);
		household.setAddress(address);
		household.setPeopleType(peopleType);
		household.setCompany(company);
		household.setDepartment(department);
		household.setIdentityType(identityType);
		household.setCredentialType(credentialType);
		household.setCredentialNo(credentialNo);
		//获取小区信息
		//处理图片的路径
		household = dealHouseholdBeanImg(household);
		household.setCreateTime(new Date());
		household.setExt3(ext3);
		household.setExt4(ext4);
		household.setExt5(ext5);
		household.setNation(nation);
		household.setHouseOwnerName(houseOwnerName);
		household.setHouseOwnerCredentialNo(houseOwnerCredentialNo);
		household.setHouseOwnerMobile(houseOwnerMobile);
		household.setFamilyMembers(memberRelation);
		if(isNew) {
			householdDao.insert(household);
			//根据手机获取用户，如果系统用户已存在，则关联起来，如果没有则新建
			UserEntity userEntity = userService.getUserByMobile(loginMobile);
			if(null == userEntity){
				//查找住户的小区
				String rid = "";
				Residence residence = residenceService.getResidenceByHouseId(houseId);
				if(null != residence) {
					rid = residence.getId().toString();
				}
				//添加相关登录用户记录
				userService.insertUser(generateUser(household,rid));
			}else{
				household.setUserId(userEntity.getId());
			}
			householdDao.updateByPrimaryKeySelective(household);

		}else {
			householdDao.updateByPrimaryKeySelective(household);
		}

		carsMapper.deleteCarsByHid(household.getId());
		if(!StringUtils.isEmpty(carInfos)) {
			carInfos = carInfos.replace("\"","").replace("[","").replace("]","");
			String[] cars = carInfos.split(",");
			List<Cars> carsList = new ArrayList<>();
			Cars car = null;
			for(String plateNum : cars) {
				car = new Cars();
				car.setHouseholdId(household.getId());
				if(StringUtils.isEmpty(plateNum)) {
					continue;
				}
				car.setPlateNum(plateNum);
				carsList.add(car);
			}
			if(null != carsList && carsList.size() > 0) {
				carsMapper.insertBatch(carsList);
			}
		}


		return household;
	}

	/**
	 * 根据住户生成用户
	 * @param householdBean
	 */
	private UserEntity generateUser(HouseholdBean householdBean,String rid) {
		UserEntity userEntity = new UserEntity();
		userEntity.setName(householdBean.getHouseholdName());
		userEntity.setPassword("111111");
		userEntity.setLoginName(householdBean.getMobile());
		userEntity.setMobile(householdBean.getMobile());
		userEntity.setAuthStatus("0");
		userEntity.setStatus("1");
		userEntity.setCreateTime(new Date());
		userEntity.setRid(rid);
		return userEntity;
	}


	@Transactional
	@Override
	public void updateHousehold(HouseholdBean householdEntity) {
		String tags = "";
		if(!StringUtils.isEmpty(householdEntity.getTags())){
			tags = String.join(",", householdEntity.getTags());
		}

		//处理图片的路径
		//修改住户信息时，如果未重新上传图片，则当前路径是新路径，如果重新上传，则转移覆盖
		//尴尬的ext6 如果是个二进制，则处理，如果未重新读照片，怎么弄？看长度吧，长度大于100就认为重新读卡了
		householdEntity = dealHouseholdBeanImg(householdEntity);

		Households households = new Households();
		BeanCopier copier = BeanCopier.create(HouseholdBean.class, Households.class, false);
		copier.copy(householdEntity, households, null);
		households.setExt4(tags);
		households.setModifyTime(new Date());
		householdDao.updateByPrimaryKey(households);

		//删除车辆
		carsMapper.deleteCarsByHid(householdEntity.getId());
		//添加车辆
		if(null != householdEntity.getCarInfos() && householdEntity.getCarInfos().size() > 0) {
			List<Cars> carsList = new ArrayList<>();
			Cars car = null;
			for(String plateNum : householdEntity.getCarInfos()) {
				car = new Cars();
				car.setHouseholdId(households.getId());
				car.setPlateNum(plateNum);
				carsList.add(car);
			}
			if(carsList.size() > 0) {
				carsMapper.insertBatch(carsList);
			}
		}

//		//解析车辆信息
//		if(null != householdEntity.getCarInfos() && householdEntity.getCarInfos().size() > 0) {
//			List<Cars> originalCars = carsMapper.getCarsByHid(householdEntity.getId());
//			List<String> plateNums = new ArrayList<>();
//			List<String> deleteCars = new ArrayList<>();
//			for(Cars car:originalCars) {
//				plateNums.add(car.getPlateNum());
//				//需要删除的车辆
//				if(!householdEntity.getCarInfos().contains(car.getPlateNum())) {
//					deleteCars.add(car.getId().toString());
//				}
//			}
//
//			//删除车辆
//			if(deleteCars.size() > 0) {
//				carsMapper.deleteCars(deleteCars);
//			}
//
//			List<Cars> carsList = new ArrayList<>();
//			Cars car = null;
//			for(String plateNum : householdEntity.getCarInfos()) {
//				//新的不包含在旧的里面，则新增
//				if(!plateNums.contains(plateNum)){
//					car = new Cars();
//					car.setHouseholdId(households.getId());
//					car.setPlateNum(plateNum);
//					carsList.add(car);
//				}
//			}
//			if(carsList.size() > 0) {
//				carsMapper.insertBatch(carsList);
//			}
//		}
	}

	@Override
	public void deleteHouseholds(List<String> hid) {
		//删除住户
		householdDao.deleteHousehold(hid);
		//删除车辆信息
		carsMapper.deleteCarsByHids(hid);
	}

	@Override
	public List<HouseholdBean> getHouseHoldByUserId(Integer uid) {
		return householdDao.getHouseHoldByUserId(uid);
	}

	@Override
	public void authHousehoulds(List<String> id) {
		householdDao.authHousehoulds(id);
	}

	@Override
	public List<HouseholdBean> getHouseholdList(List<String> rids) {
		return householdDao.getHouseholdList(rids);
	}

	@Override
	public Households getHouseholdByUidAndHid(Integer uid, Integer houseId) {
		return householdDao.getHouseholdByUidAndHid(uid,houseId);
	}

	@Override
	public void insertBatch(List<HouseholdBean> list) {
		householdDao.insertBatch(list);
	}

	@Override
	public void insertBatch1(List<HouseholdBean> list) {
		householdDao.insertBatch1(list);
	}

	@Override
	public List<HouseholdBean> getHouseholdByMobile(String mobile) {
		return householdDao.getHouseholdByMobile(mobile);
	}

	@Override
	public List<Households> getHouseholdByIds(List<String> ids) {
		return householdDao.getHouseholdByIds(ids);
	}

	@Override
	public void updateByPrimaryKeySelective(HouseholdBean household) {
		household.setModifyTime(new Date());
		householdDao.updateByPrimaryKeySelective(household);
	}

	private String getExt4Str(JSONArray jsonArray){
		List<String> list = new ArrayList<String>();
		for(int i = 0;i<jsonArray.size();i++) {
			list.add(jsonArray.getString(i));
		}
		if(list.size() >0){
			return String.join(",", list);
		}
		return "";
	}

	/**
	 * 处理图片的路径问题
	 * @param householdEntity
	 * @return
	 */
	private HouseholdBean dealHouseholdBeanImg(HouseholdBean householdEntity){
		//图片名称
		String picName = householdEntity.getHouseholdName() + "#M"+householdEntity.getCredentialNo()+".jpg";
		String cardPic1 = householdEntity.getHouseholdName() + "#M"+householdEntity.getCredentialNo()+"_1.jpg";
		String cardPic2 = householdEntity.getHouseholdName() + "#M"+householdEntity.getCredentialNo()+"_2.jpg";
		try {
			picName = new String(picName.getBytes("UTF-8"));
			cardPic1 = new String(cardPic1.getBytes("UTF-8"));
			cardPic2 = new String(cardPic2.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		//由于上传图片时没办法知道图片具体的业务目录，全部统一放在临时目录下
		//调用公共方法，将默认路径下的图片转移到业务目录下
		//1.人脸图片
		if(!StringUtils.isEmpty(householdEntity.getFacePic())){
			if(!householdEntity.getFacePic().contains("%23M")&&!householdEntity.getFacePic().contains("#M")) {
				householdEntity.setFacePic(FileUtil.moveFilePath(commonValues.getBasePath() + FileUtil.getFileName(householdEntity.getFacePic()),
						String.format(commonValues.getLocalPath(), householdEntity.getProjectName(), CommonConstants.FACE),
						picName));
			}else{
				householdEntity.setFacePic(FileUtil.getFileName(householdEntity.getFacePic()));
			}
		}
		//2.身份证图片
		if(!StringUtils.isEmpty(householdEntity.getIdCardImage1())) {
			if (!householdEntity.getIdCardImage1().contains("%23M") && !householdEntity.getIdCardImage1().contains("#M")) {
				householdEntity.setIdCardImage1(FileUtil.moveFilePath(commonValues.getBasePath() + FileUtil.getFileName(householdEntity.getIdCardImage1()),
						String.format(commonValues.getLocalPath(), householdEntity.getProjectName(), CommonConstants.CARD),
						cardPic1));
			} else {
				householdEntity.setIdCardImage1(FileUtil.getFileName(householdEntity.getIdCardImage1()).replace("%23M","#M"));
			}
		}
		if(!StringUtils.isEmpty(householdEntity.getIdCardImage2())) {
			if (!householdEntity.getIdCardImage2().contains("%23M") && !householdEntity.getIdCardImage2().contains("#M")) {
				householdEntity.setIdCardImage2(FileUtil.moveFilePath(commonValues.getBasePath() + FileUtil.getFileName(householdEntity.getIdCardImage2()),
						String.format(commonValues.getLocalPath(), householdEntity.getProjectName(), CommonConstants.CARD),
						cardPic2));
			} else {
				householdEntity.setIdCardImage2(FileUtil.getFileName(householdEntity.getIdCardImage2()).replace("%23M","#M"));
			}
		}
		//3.身份证上的图片 todo 怎们判断修改和新增
		if(!StringUtils.isEmpty(householdEntity.getExt6())){
			if(householdEntity.getExt6().length() > 100) {
				//将base64转成图片
				Base64Util.GenerateImage(householdEntity.getExt6(),
						String.format(commonValues.getLocalPath(),householdEntity.getProjectName(),CommonConstants.CARD_FACE) + picName);
				//不管什么图片都转成jpg
				ImageUtils.convertJPG(String.format(commonValues.getLocalPath(),householdEntity.getProjectName(),CommonConstants.CARD_FACE) + picName,
						String.format(commonValues.getLocalPath(),householdEntity.getProjectName(),CommonConstants.CARD_FACE) + picName);
				householdEntity.setExt6(picName);
			}else{
				householdEntity.setExt6(FileUtil.getFileName(householdEntity.getExt6()).replace("%23M","#M"));
			}
			//否则认为未重新读卡，不做处理
		}
		return householdEntity;
	}

	@Override
	public int[] moveHousePic(String imagePath,String targetPath) {
		String cardImg1 = "";//身份证正面
		String cardImg2 = "";//身份证反面
		String faceImg = "";//用户上传的人脸照片
		String cardFaceImg = "";//通过身份证获取的人脸照片
		String realName = "";//统一的照片名称,不含后缀
		String fileName = "";
		List<HouseholdBean> newList = new ArrayList<HouseholdBean>();
		int[] count = {0,0};
		List<HouseholdBean> list =
				householdDao.getHouseholdList(null);
		count[0] = list.size();
		for(HouseholdBean bean:list){
			if(org.apache.commons.lang3.StringUtils.isNotEmpty(bean.getHouseholdName()) && org.apache.commons.lang3.StringUtils.isNotEmpty(bean.getCredentialNo())){
				HouseholdBean newBean = new HouseholdBean();
				newBean.setId(bean.getId());
				realName= bean.getHouseholdName()+"#M"+bean.getCredentialNo();

				cardImg1 = bean.getIdCardImage1();
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(cardImg1)){
					cardImg1 = cardImg1.substring(cardImg1.lastIndexOf("/")+1,cardImg1.length());
					if(cardImg1.contains("."))
						cardImg1 = cardImg1.substring(0,cardImg1.lastIndexOf("."));
					fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg1,targetPath+File.separator+
							bean.getProjectName()+File.separator+"card",realName+"1.jpg");
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg1 + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"1.jpg");
					}
					if("".equals(fileName)){//失败加上.bmp后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg1 + ".bmp",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"1.bmp");
					}
					if("".equals(fileName)){//失败加上小区名称重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardImg1,
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"1.jpg");
					}
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardImg1 + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"1.jpg");
					}

					if (org.apache.commons.lang3.StringUtils.isNotEmpty(fileName))
						newBean.setIdCardImage1(fileName);
				}

				cardImg2 = bean.getIdCardImage2();
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(cardImg2)){
					cardImg2 = cardImg2.substring(cardImg2.lastIndexOf("/")+1,cardImg2.length());
					if(cardImg2.contains("."))
						cardImg2 = cardImg2.substring(0,cardImg2.lastIndexOf("."));
					fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg2,targetPath+File.separator+
							bean.getProjectName()+File.separator+"card",realName+"2.jpg");
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg2 + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"2.jpg");
					}
					if("".equals(fileName)){//失败加上.bmp后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardImg2 + ".bmp",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"2.bmp");
					}
					if("".equals(fileName)){//失败加上小区名称重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardImg2,
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"2.jpg");
					}
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardImg2 + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card",realName+"2.jpg");
					}

					if (org.apache.commons.lang3.StringUtils.isNotEmpty(fileName))
						newBean.setIdCardImage2(fileName);
				}

				faceImg = bean.getFacePic();
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(faceImg)){
					faceImg = faceImg.substring(faceImg.lastIndexOf("/")+1,faceImg.length());
					if(faceImg.contains("."))
						faceImg = faceImg.substring(0,faceImg.lastIndexOf("."));
					fileName = FileUtil.moveFilePath(imagePath+File.separator+faceImg,targetPath+File.separator+
							bean.getProjectName()+File.separator+"face",realName+".jpg");
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+faceImg + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"face",realName+".jpg");
					}
					if("".equals(fileName)){//失败加上.bmp后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+faceImg + ".bmp",
								targetPath+File.separator+bean.getProjectName()+File.separator+"face",realName+".bmp");
					}
					if("".equals(fileName)){//失败加上小区名称重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+faceImg,
								targetPath+File.separator+bean.getProjectName()+File.separator+"face",realName+".jpg");
					}
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+faceImg + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"face",realName+".jpg");
					}

					if (org.apache.commons.lang3.StringUtils.isNotEmpty(fileName))
						newBean.setFacePic(fileName);
				}

				cardFaceImg = bean.getExt6();
				if(org.apache.commons.lang3.StringUtils.isNotEmpty(cardFaceImg)){
					cardFaceImg = cardFaceImg.substring(cardFaceImg.lastIndexOf("/")+1,cardFaceImg.length());
					if(cardFaceImg.contains("."))
						cardFaceImg = cardFaceImg.substring(0,cardFaceImg.lastIndexOf("."));
					fileName = FileUtil.moveFilePath(imagePath+File.separator+cardFaceImg,targetPath+File.separator+
							bean.getProjectName()+File.separator+"card_face",realName+".jpg");
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardFaceImg + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card_face",realName+".jpg");
					}
					if("".equals(fileName)){//失败加上.bmp后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+cardFaceImg + ".bmp",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card_face",realName+".bmp");
					}
					if("".equals(fileName)){//失败加上小区名称重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardFaceImg,
								targetPath+File.separator+bean.getProjectName()+File.separator+"card_face",realName+".jpg");
					}
					if("".equals(fileName)){//失败加上.jpg后缀重新操作一次
						fileName = FileUtil.moveFilePath(imagePath+File.separator+bean.getProjectName()+File.separator+cardFaceImg + ".jpg",
								targetPath+File.separator+bean.getProjectName()+File.separator+"card_face",realName+".jpg");
					}

					if (org.apache.commons.lang3.StringUtils.isNotEmpty(fileName))
						newBean.setExt6(fileName);
				}
				if(null != newBean.getIdCardImage1()||null != newBean.getIdCardImage2()||null != newBean.getFacePic()||null != newBean.getExt6()){
					householdDao.updateByPrimaryKeySelective(newBean);
					newList.add(newBean);
				}

			}
		}

		count[1] = newList.size();
		return count;
	}

	@Override
	public List<HouseholdBean> getHouseholdByHid(Integer hid) {
		return householdDao.getHouseholdByHid(hid);
	}

}
